package com.limu.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.limu.boot.bean.Car;
import com.limu.boot.bean.Pet;
import com.limu.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/*
* 1.配置类里面使用@Bean标注在方法上给容器注册组件，默认也是单实例的
*2.配置类本身也是组件
* 3.proxyBeanMethods：代理bean的方法
*       FULL    (proxyBeanMethods = true)
*       LITE    (proxyBeanMethods = false)
*       组件依赖必须使用FULL模式默认，其他默认是否Lite模式
*
* 4.@Import({User.class, DBHelper.class})
*       给容器中自动筹建处这两个类型的组件,默认组件的名字就是全类名
*
*
* */

//@ConditionalOnBean(name = "tom")
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false)  //告诉springboot这是一个配置类  == 配置文件
@ConditionalOnMissingBean(name = "tom")
@ImportResource("classpath:beans.xml")
@EnableConfigurationProperties(Car.class)
//1.开启Car配置绑定功能 2.把这个Car这个组件自动注册到容器中
public class MyConfig {

    @Bean  //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型，返回的值，就是组件在容器的实例
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        //user组件依赖了Pet组件
        zhangsan.setPet(tomcatPet());
        return new User("张三",18);
    }

    //@Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }

}
